<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends Controller {
    //AJAX删除商品图片相册
    public function ajaxDelPic()
    {
        $picId = I('get.picid');
        // 根据ID从硬盘上数据删除中删除图片
        $gpModel = D('goods_pic');
        $pic = $gpModel->field('pic,sm_pic,mid_pic,big_pic')->find($picId);
        // 从硬盘删除图片
        deleteImage($pic);
        // 从数据库中删除记录
        if($aa = $gpModel->delete($picId)){
            $this->ajaxReturn($aa);
        }
    }
    public function add(){
        if(IS_POST){
            $model = D('goods');
            if($mm = $model->create(I('psot.'),1)){ //1表示添加  2表示修改
                //插入到数据库中

                if($model->add()){
                    $this->success('商品添加成功!',U('lst'));
                    exit;
                }
            }
            $error = $model->getError();
            $this->error($error);
        }
        //取出会员级别
        $mlModel = D("member_level");
        $mlData = $mlModel->select();
        //取出品牌
        $brandModel = M('Brand');
        $brandData = $brandModel->select();
        // 设置页面信息
        $this->assign(array(
            'mlData'=>$mlData,
            'brandData'=>$brandData,
            '_page_title' => '添加新商品',
            '_page_btn_name' => '商品列表',
            '_page_btn_link' => U('lst'),
        ));
        //显示表单
        $this->display('goodsAdd');
    }

    public function lst(){
        $model = D("Goods");
        $data = $model->search(5);
        $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
        ));
        echo $model->getLastSql();
        // 设置页面信息
        $this->assign(array(
            '_page_title' => '商品列表',
            '_page_btn_name' => '添加新商品',
            '_page_btn_link' => U('add'),
        ));
        $this->display('goodsList');
    }
    public function edit($id){
        $model = D('goods');
        $data = $model->find($id);
        if(IS_POST){
            if($mm = $model->create(I('psot.'),2)){ //1表示添加  2表示修改
                // save()的返回值是，如果失败返回false,如果成功返回受影响的条数【如果修改后和修改前相同就会返回0
                if(false !== $model->save()){
                    $this->success('商品修改成功!',U('lst?p='.I('get.p')));
                    exit;
                }
            }
            $error = $model->getError();
            $this->error($error);
        }
        //取出相册中现有的图片
        $gpModel = D('goods_pic');
        $gpData = $gpModel->field('id,mid_pic')->where(array('goods_id'=>array('eq',$id)))->select();
        //取出会员级别
        $mlModel = D("member_level");
        $mlData = $mlModel->select();
        //取出这件商品已经设置好的会员价格
        $mpModel = D("member_price");
        $mpData = $mpModel->where(array('goods_id'=>array('eq',$id)))->select();
        //把这个二维数组转化为一维数组
        $_mpData = array();
        foreach($mpData as $k => $v){
            $_mpData[$v['level_id']] = $v['price'];
        }
        //取出品牌
        $brandModel = M('Brand');
        $brandData = $brandModel->select();
        // 设置页面信息
        $this->assign(array(
            'mlData'=>$mlData,
            'mpData'=>$_mpData,
            'gpData'=>$gpData,
            'brandData'=>$brandData,
            '_page_title' => '修改商品',
            '_page_btn_name' => '商品列表',
            '_page_btn_link' => U('lst'),
        ));
        //显示表单
        $this->assign('data',$data);
        $this->display();
    }
    public function delete(){
        $model = D('Goods');
        if(false !== $model->delete(I('get.id'))){
            $this->success('删除成功！',U('lst',array('p'=>I('get.p',1))));
        }else{
            $this->error('删除失败！原因：'.$model->getError());
        }
    }
}




















